﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Workflow.DriveEvents
{
    public class IssuedToDepartmentDriveEvent : IWorkflowDriveEvent
    {
        public WorkflowDriveRecord Drive(
            WorkflowInstance instance,
            WorkflowMetadata workflow, 
            ActivityMetadata activity, 
            EventMetadata @event,
            WorkflowDriveRecord currentRecord, 
            Guid dataId, 
            Core.User sender,
            IEnumerable<Guid> receivers)
        {
            // 关闭上一步
            if (currentRecord != null)
            {
                currentRecord.IsActivated = false;
            }

            WorkflowDriveRecord parent = new WorkflowDriveRecord();
            parent.Id = Guid.NewGuid();
            parent.InstanceId = instance.Id;
            parent.WorkflowId = instance.WorkflowId;
            parent.ActivityId = @event.ToActivityId;
            parent.EventId = @event.Id;
            parent.SenderId = sender.Id;
            parent.DrivedDate = DateTime.Now;
            parent.RecordId = dataId;
            parent.IsActivated = true;
            parent.Children = new List<WorkflowDriveRecord>();
            foreach (var item in receivers)
            {
                WorkflowDriveRecord driveRecord = new WorkflowDriveRecord();
                driveRecord.Id = Guid.NewGuid();
                driveRecord.InstanceId = instance.Id;
                driveRecord.WorkflowId = instance.WorkflowId;
                driveRecord.ActivityId = @event.ToActivityId;
                driveRecord.EventId = @event.Id;
                driveRecord.SenderId = sender.Id;
                driveRecord.ReceiverId = item;
                driveRecord.ReceiverType = WfReceiverType.Department;
                driveRecord.DrivedDate = DateTime.Now;
                driveRecord.RecordId = dataId;
                driveRecord.IsActivated = true;
                parent.Children.Add(driveRecord);
            }
            return parent;
        }
    }
}
